package common.utils.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class fileDownloadReturnPathHandler implements Runnable {

	private String httpPath;
	private Handler progressHandler;
	private String fileName;

	public fileDownloadReturnPathHandler(String httpPath, Handler progressHandler, String fileName) {
		this.httpPath = httpPath;
		this.progressHandler = progressHandler;
		this.fileName = fileName;
	}

	private File fileDirectory = getImageCacheDirectory();

	@Override
	public void run() {
		File imgFile = new File(fileDirectory, fileName);
		if (imgFile.exists()) {
			imgFile.delete();
		}
		updataDownload(httpPath);

	}

	private void updataDownload(String httpPath) {
		FileOutputStream fos = null;
		InputStream fis = null;
		HttpURLConnection httpUrlConnection = null;
		File imageFile = null;
		try {
			URL httpUrl = new URL(httpPath);

			if (!fileDirectory.exists()) {
				fileDirectory.mkdirs();
			}
			imageFile = new File(fileDirectory, fileName);
			httpUrlConnection = (HttpURLConnection) httpUrl.openConnection();
			int httpState = 404;
			try {
				httpState = httpUrlConnection.getResponseCode();
			} catch (ConnectException e) {
				Log.e("updata download", e.getMessage());
			}
			if (httpState < 400) {
				if (imageFile.exists()) {
					imageFile.delete();
				}
				fos = new FileOutputStream(imageFile);
				httpUrlConnection.connect();
				fis = httpUrlConnection.getInputStream();
				int i = 0;
				byte[] b = new byte[64];
				while ((i = fis.read(b)) != -1) {
					fos.write(b, 0, i);
				}
			} else {

			}
		} catch (MalformedURLException e) {
			Log.e("updata download", e.getMessage());
		} catch (FileNotFoundException e) {
			Log.e("updata download", e.getMessage());
		} catch (IOException e) {
			Log.e("updata download", e.getMessage());
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (fos != null) {
					fos.flush();
					fos.close();
				}
				httpUrlConnection.disconnect();
			} catch (IOException e) {
				Log.e("updata download", e.getMessage());
			}
		}

		sendMsg(imageFile.getPath());
	}

	private void sendMsg(String path) {
		Message msg = new Message();
		msg.obj = path;
		progressHandler.sendMessage(msg);
	}

	private File getImageCacheDirectory() {
//		if(!isSdcardPrepared()){
		return new File(Environment.getExternalStorageDirectory(), "3G-explorer");
//		}else{
//			return new File(Environment.getExternalStorageDirectory(), "/service/com.techmore.pls/xmlCache");
//		}
	}

//	private boolean isSdcardPrepared(){
//		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
//	}

}
